package LeetCode;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class LC_107_BinaryTreeLevelOrderTraversalII {
    public static void main(String[] args) {

    }

    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    static class Solution {
        public List<List<Integer>> levelOrderBottom(TreeNode root) {
            List<List<Integer>> list = new LinkedList<>();
            Queue<TreeNode> queue = new LinkedList<>();
            if (root == null) return list;
            queue.offer(root);
            while (!queue.isEmpty()) {
                int num = queue.size();
                List<Integer> levelList = new LinkedList<>();
                for (int i = 0; i < num; i++) {
                    if (queue.peek().left != null) queue.offer(queue.peek().left);
                    if (queue.peek().right != null) queue.offer(queue.peek().right);
                    levelList.add(queue.poll().val);
                }
                list.add(0, levelList);
            }
            return list;
        }
    }

}
